
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <assert.h>
#include "wxapi.h"

string func="";
string sAppId="wxc5ab743558b1a85c";
string sSeceret="b84c2dcc2c0bad926181d938815b7943";
string sAccessToken="";
string sContent="";
string sToUserList="ocB_hjqMAfYJc4DMBN4fc1wM1YiY";//split by , 
#define touseropenid "ocB_hjqMAfYJc4DMBN4fc1wM1YiY"
void ShowUsage( const char * func )
{
	printf("%s -f <func> \n", func );
	printf("-f getaccesstoken -a <appid> -s <seceret> \n");
	printf("-f sendtxt -t <access_token> -c <content> -l <touser list>\n");
	printf("-f sendimg -t <access_token> -p <img path> \n");
}

void getaccesstoken();
void sendtxt();
void sendimg();

int main( int argc, char * argv[] )
{
	if ( argc<2 )
	{
		ShowUsage( argv[0] );
		return 0;
	}

	int opt =-1;

	while ( (opt=getopt( argc, argv, "f:a:s:t:c:l:v") ) != -1 )
	{
		switch( opt )
		{
			case 'f':
				func = optarg;
				break;

			case 'a':
				sAppId = optarg;
				break;

			case 's':
				sSeceret = optarg;
				break;

			case 't':
				sAccessToken = optarg;
				break;	
			
			case 'l':
				sToUserList = optarg;
				break;
			
			case 'c':
				sContent = optarg;
				break;

			case 'v':
			default:
				ShowUsage(argv[0]);
				exit(0);
		};
	}
	printf("%s\n", func.c_str());
	if ( func == "getaccesstoken" )
	{
		getaccesstoken();
	}
	else if ( func == "sendtxt" )
	{
		sendtxt();
	}
	else if ( func == "sendimg" )
	{
		sendimg();
	}
	else
	{
		printf("unsupport func %s\n", func.c_str() );
	}
	return 0;
}

void getaccesstoken()
{
	sAccessToken = "";
	int iExpireTime = -1;
	int ret = WeixinApi::GetAccessToken( sAppId, sSeceret, sAccessToken, iExpireTime );

	printf( "%s ret %d appid %s seceret %s accesstoken %s expiretime %d\n", 
			__func__, ret, sAppId.c_str(), sSeceret.c_str(), sAccessToken.c_str(), iExpireTime );
}

void sendtxt()
{
	sAccessToken = "";
	int iExpireTime = -1;
	int ret2 = WeixinApi::GetAccessToken( sAppId, sSeceret, sAccessToken, iExpireTime );
	printf("ret2 %d\n", ret2 );
	string sStatusOut;
	vector<string> toUserList;
	toUserList.push_back(sToUserList);
	int ret = WeixinApi::SendTxt( sAccessToken, sContent, toUserList, true, "", sStatusOut );
	
	printf("%s ret %d sAccessToken %s sContent %s touserlist %s statusout %s\n", __func__, ret, sAccessToken.c_str(), sContent.c_str(), sToUserList.c_str(), sStatusOut.c_str() );
}

void sendimg()
{
	sAccessToken = "";
	int iExpireTime = -1;
	int ret2 = WeixinApi::GetAccessToken( sAppId, sSeceret, sAccessToken, iExpireTime );
	printf("ret2 %d\n", ret2 );
	string sStatusOut;
	string sImgBuf;
	vector<string> toUserList;
	toUserList.push_back(touseropenid);
	

	int ret = WeixinApi::SendImg( sAccessToken, "/home/dirkcai/workspace/gtd/src/weixinagent/test.jpg", toUserList, true, "", sStatusOut );
	
		printf("%s ret %d sAccessToken %s touserlist %s statusout %s\n", __func__, ret, sAccessToken.c_str(), sToUserList.c_str(), sStatusOut.c_str() );
}
